home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualFolder.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  8.4 KB  |  351 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualFolder.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __ERRORS__
  15. #include <Errors.h>
  16. #endif
  17.  
  18. #ifndef __VIRTUALFOLDER__
  19. #include "VirtualFolder.h"
  20. #endif
  21.  
  22. #ifndef __VIRTUALHSFMACFILE__
  23. #include "VirtualHFSMacFile.h"
  24. #endif
  25.  
  26. #ifndef    __DEBUGGINGEAR__
  27. #include "DebuggingGear.h"
  28. #endif
  29.  
  30. /***********************************|****************************************/
  31.  
  32. #pragma segment VirtualFolder
  33.  
  34. /***********************************|****************************************/
  35.  
  36. ImplementList ( TVirtualMacFile, TMacFileList, true );
  37. ImplementList ( TVirtualFolder, TFolderList, true );
  38.  
  39. /***********************************|****************************************/
  40.  
  41. TVirtualFolder::TVirtualFolder(const FSSpec& spec, Boolean mimicActualHFS ):
  42.     THandleObject (),
  43.     fSpec ( spec ),
  44.     fFileList ( new TMacFileList ),
  45.     fFolderList ( new TFolderList )
  46. {
  47.     if ( mimicActualHFS )
  48.     {
  49.         TRY
  50.         {
  51.             InheritDown ( spec );
  52.         }
  53.         EXCEPTION
  54.         {
  55.         }
  56.         ENDEXCEPTION
  57.     }
  58. }
  59.  
  60. /***********************************|****************************************/
  61.  
  62. TVirtualFolder::TVirtualFolder(const Str255 name):
  63.     THandleObject (),
  64.     fFileList ( new TMacFileList ),
  65.     fFolderList ( new TFolderList )
  66. {
  67.     fSpec.vRefNum = -1;
  68.     fSpec.parID = 2;
  69.     SetFolderName(name);
  70. }
  71.  
  72. /***********************************|****************************************/
  73.  
  74. TVirtualFolder::TVirtualFolder(const char* fileName) :
  75.     THandleObject (),
  76.     fFileList ( new TMacFileList ),
  77.     fFolderList ( new TFolderList )
  78. {
  79.     fSpec.vRefNum = -1;
  80.     fSpec.parID = 2;
  81.  
  82.     SetFolderName( fileName );
  83. }
  84.  
  85. /***********************************|****************************************/
  86.  
  87. TVirtualFolder::~TVirtualFolder()
  88. {
  89. #if debug
  90.     if (steveFlag.Flag(18))
  91.         steve << "TVirtualFolder::~TVirtualFolder, @ " << (long) this << ", deleting " << FileCount() << " sub-files, " << FolderCount() << " sub-folders of folder." << endl;
  92. #endif
  93.  
  94.     delete fFileList;
  95.     delete fFolderList;
  96. }
  97.  
  98. /***********************************|****************************************/
  99.  
  100. OSErr TVirtualFolder::AddFile(TVirtualMacFile* file)
  101. {
  102.     if ( file )
  103.     {
  104.         Str31 name;
  105.         file->GetFileName(name);
  106.     
  107.         if (IsFileFolderNameUnique(name))
  108.         {
  109.             fFileList->Append(file);
  110.             return noErr;
  111.         }
  112.         else
  113.             return dupFNErr;
  114.     }
  115.     else
  116.         return nilHandleErr;
  117. }
  118.  
  119. /***********************************|****************************************/
  120.  
  121. OSErr TVirtualFolder::AddFolder(TVirtualFolder* folder)
  122. {
  123.     if ( folder )
  124.     {
  125.         Str31 name;
  126.         folder->GetFolderName(name);
  127.     
  128.         if    (IsFileFolderNameUnique(name))
  129.         {
  130.             fFolderList->Append(folder);
  131.             return noErr;
  132.         }
  133.         else
  134.             return dupFNErr;
  135.     }
  136.     else
  137.         return nilHandleErr;
  138. }
  139.  
  140. /***********************************|****************************************/
  141.  
  142. Boolean TVirtualFolder::IsFileFolderNameUnique(const StringPtr theName) const
  143. {
  144.     Str31 itemName;
  145.     unsigned long index, count;
  146.  
  147.     for ( index = 1, count = FileCount(); index <= count; index++ )
  148.     {
  149.         GetIndexFile(index)->GetFileName(itemName);
  150.  
  151.         if (PLstrcmp(itemName, theName) == 0)
  152.             return false;
  153.     }
  154.  
  155.     for (index = 1, count = FolderCount(); index<= count; index++)
  156.     {
  157.         GetIndexFolder(index)->GetFolderName(itemName);
  158.     
  159.         if (PLstrcmp(itemName, theName) == 0)
  160.             return false;
  161.     }
  162.  
  163.     return true;
  164. }
  165.  
  166. /***********************************|****************************************/
  167.  
  168. unsigned long TVirtualFolder::TotalSize() const
  169. {
  170.     long logEofDF, logEofRF;
  171.     unsigned long index, count, tSize = 0;
  172.  
  173.     for ( index = 1, count = FileCount(); index <= count; index++)
  174.     {
  175.         const TVirtualMacFile* theFile = fFileList->Get(index);
  176.         theFile->GetEnd (logEofDF,TVirtualMacFile::kData);
  177.         theFile->GetEnd (logEofRF,TVirtualMacFile::kResource);
  178.         tSize += logEofRF + logEofDF;
  179.     }
  180.  
  181.     for (index = 1, count = FolderCount(); index <= count; index++)
  182.         tSize += fFolderList->Get(index)->TotalSize();
  183.  
  184.     return tSize;
  185. }
  186.  
  187. /***********************************|****************************************/
  188.  
  189. OSErr TVirtualFolder::WriteToDisk ( short vRefNum, long parID )
  190. {
  191.     fSpec.vRefNum = vRefNum;
  192.     fSpec.parID = parID;
  193.     long myDirID = 0;
  194.     
  195.     OSErr error = FSpDirCreate ( &fSpec, smRoman, &myDirID );
  196.     
  197.     if ( error == noErr || error == dupFNErr )
  198.         error = WriteContentsToDisk ( vRefNum, myDirID );
  199.  
  200.     return error;
  201. }
  202.  
  203. /***********************************|****************************************/
  204.  
  205. OSErr TVirtualFolder::WriteContentsToDisk ( short vRefNum, long parID )
  206. {
  207.     OSErr error = noErr;
  208.     unsigned long index, count;
  209.     FSSpec spec;
  210.     spec.vRefNum = vRefNum;
  211.     spec.parID = parID;
  212.  
  213.     for ( index = 1, count = FileCount (); index <= count && error == noErr; index++ )
  214.     {
  215.         TVirtualMacFile* file = GetIndexFile ( index );
  216.         file->GetFileName ( spec.name );
  217.         error = file->WriteToDisk ( spec );
  218.     }
  219.  
  220.     for ( index = 1, count = FolderCount (); index <= count && error == noErr; index++ )
  221.     {
  222.         TVirtualFolder* folder = GetIndexFolder ( index );
  223.         error = folder->WriteToDisk ( vRefNum, parID );
  224.     }
  225.  
  226.     return error;
  227. }
  228.  
  229. /***********************************|****************************************/
  230.  
  231. void TVirtualFolder::SetFolderName(const Str255 name)
  232. {
  233.     PLstrcpy(fSpec.name,name);
  234. }
  235.  
  236. /***********************************|****************************************/
  237.  
  238. void TVirtualFolder::SetFolderName(const char* name)
  239. {
  240.     strcpy((char*) &fSpec.name[1], name);
  241.     fSpec.name[0] = strlen(name);
  242. }
  243.  
  244. /***********************************|****************************************/
  245.  
  246. void TVirtualFolder::GetFolderName(StringPtr name) const
  247. {
  248.     PLstrcpy(name,fSpec.name);
  249. }
  250.  
  251. /***********************************|****************************************/
  252.  
  253. void TVirtualFolder::InheritDown ( const FSSpec& fileSpec ) 
  254. {
  255.     CInfoPBRec paramBlock;
  256.     short fileIndex = 0;
  257.  
  258.     paramBlock.dirInfo.ioVRefNum = fileSpec.vRefNum;
  259.     paramBlock.dirInfo.ioDrDirID = fileSpec.parID;
  260.     paramBlock.dirInfo.ioFDirIndex = fileIndex;
  261.     paramBlock.dirInfo.ioNamePtr = fileSpec.name;
  262.     OSErr error = PBGetCatInfo(¶mBlock, false);
  263.     FAILOSErr ( error );
  264.     
  265.     long folderRefNum = paramBlock.dirInfo.ioDrDirID;    // Get Folder RefNum
  266.     
  267.     while ( !error )
  268.     {
  269.         FSSpec newSpec;
  270.         paramBlock.dirInfo.ioVRefNum = fileSpec.vRefNum;
  271.         paramBlock.dirInfo.ioDrDirID = folderRefNum;
  272.         paramBlock.dirInfo.ioFDirIndex = ++fileIndex;
  273.         paramBlock.dirInfo.ioNamePtr = newSpec.name;
  274.         error = PBGetCatInfo ( ¶mBlock, false );
  275.  
  276.         if ( !error && !BitAnd ( paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible ) ) 
  277.         {
  278.             newSpec.vRefNum = paramBlock.dirInfo.ioVRefNum;
  279.             newSpec.parID = folderRefNum;
  280.  
  281.             if ( BitTst ( ¶mBlock.dirInfo.ioFlAttrib, 3 ) )
  282.                 AddFolder ( new TVirtualFolder ( newSpec ) );
  283.             else 
  284.                 AddFile ( new TVirtualHFSMacFile ( newSpec ) );
  285.         }
  286.     }
  287. }
  288.  
  289. /***********************************|****************************************/
  290.  
  291. OSErr TVirtualFolder::SetFinderInfo (const FInfo& finderInfo)
  292. {
  293.     return ::FSpSetFInfo ( &fSpec, &finderInfo );
  294. }
  295.  
  296. /***********************************|****************************************/
  297.  
  298. OSErr TVirtualFolder::GetFinderInfo (FInfo& finderInfo)  const
  299. {
  300.     return ::FSpGetFInfo ( &((TVirtualFolder*)this)->fSpec, &finderInfo );
  301. }
  302.  
  303. /***********************************|****************************************/
  304.  
  305. OSErr TVirtualFolder::GetDate (unsigned long& dateTime,TVirtualMacFile::WhichDateType whichDate) const
  306. {
  307.     CInfoPBRec paramBlock;
  308.  
  309.     paramBlock.dirInfo.ioVRefNum = fSpec.vRefNum;
  310.     paramBlock.dirInfo.ioDrParID = fSpec.parID;
  311.     paramBlock.dirInfo.ioDrDirID = fSpec.parID;
  312.     paramBlock.dirInfo.ioFDirIndex = 0;
  313.     paramBlock.dirInfo.ioNamePtr = fSpec.name;
  314.     OSErr error = PBGetCatInfo(¶mBlock, false);
  315.  
  316.     if (whichDate == TVirtualMacFile::kCreationDate)
  317.         dateTime = paramBlock.dirInfo.ioDrCrDat;
  318.     else
  319.         dateTime = paramBlock.dirInfo.ioDrMdDat;
  320.  
  321.     return error;
  322. }
  323.  
  324. /***********************************|****************************************/
  325.  
  326. OSErr TVirtualFolder::SetDate (unsigned long dateTime,TVirtualMacFile::WhichDateType whichDate)
  327. {
  328.     CInfoPBRec paramBlock;
  329.  
  330.     paramBlock.dirInfo.ioVRefNum = fSpec.vRefNum;
  331.     paramBlock.dirInfo.ioDrParID = fSpec.parID;
  332.     paramBlock.dirInfo.ioDrDirID = fSpec.parID;
  333.     paramBlock.dirInfo.ioFDirIndex = 0;
  334.     paramBlock.dirInfo.ioNamePtr = fSpec.name;
  335.     OSErr error = PBGetCatInfo(¶mBlock, false);
  336.  
  337.     if (whichDate == TVirtualMacFile::kCreationDate)
  338.         paramBlock.dirInfo.ioDrCrDat = dateTime;
  339.     else
  340.         paramBlock.dirInfo.ioDrMdDat = dateTime;
  341.  
  342.     paramBlock.dirInfo.ioDrDirID = fSpec.parID;
  343.  
  344.     if (error == noErr)
  345.         error = PBSetCatInfo(¶mBlock, false);
  346.         
  347.     return error;
  348. }
  349.  
  350. /***********************************|****************************************/
  351.